package cn.th.controller;


import cn.th.common.R;
import cn.th.dto.AdDto;
import cn.th.entity.Admin;
import cn.th.entity.AdvLabel;
import cn.th.service.IAdvLabelService;
import cn.th.service.IAdvertisingService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

/**
 * <p>
 * 广告表 前端控制器
 * </p>
 *
 * @author cn.th
 * @since 2024-03-30
 */
@RestController
@RequestMapping("/ad")
@Slf4j
public class AdvertisingController {

    @Autowired
    private IAdvertisingService advertisingService;

    @Autowired
    private IAdvLabelService advLabelService;
    /**
     * 查询广告
     * @param page 页面
     * @param limit 每页的数据
     * @param keywordCount 关键词等级
     * @param title 标题
     * @param type 类型
     * @param sort 排序
     * @return 返回
     */
    @GetMapping("/list")
    public R<Map<String,Object>> list(@RequestParam(value = "page") int page,
                                      @RequestParam(value = "limit") int limit,
                                      @RequestParam(value = "keywordCount", required = false) String keywordCount,
                                      @RequestParam(value = "title", required = false) String title,
                                      @RequestParam(value = "type", required = false) String type,
                                      @RequestParam(value = "sort", required = false) String sort,
                                      @RequestParam(value = "publishName", required = false) String publishName,
                                      @RequestParam(value = "status", required = false) Integer status){

        Map<String,Object> data = advertisingService.queryAd(page, limit, keywordCount, title, type, sort, publishName, status);

        return R.success(data);
    }

    /**
     * 广告细节 一般用于显示
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public R<Map<String,Object>> detail(@RequestParam(value = "id") Long id){
        Map<String,Object> data = advertisingService.queryById(id);
        return R.success(data);
    }

    /**
     * 新增广告
     * @param adDto
     * @return
     */
    @PostMapping("/create")
    public R<?> create(@RequestBody AdDto adDto){
        log.info("新增广告内容为：{}",adDto.toString());
        advertisingService.saveAd(adDto);
        return R.success("新增广告成功");
    }

    /**
     * 删除广告
     * @param id
     * @return
     */
    @Transactional
    @DeleteMapping
    public R<String> delete(Long id){
        log.info("删除广告信息，id为：{}",id);

        //删除广告表的数据
        advertisingService.removeById(id);

        //删除标签表中的数据----adv_label
        LambdaQueryWrapper<AdvLabel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(AdvLabel::getAdId,id);
        advLabelService.remove(lambdaQueryWrapper);

        return R.success("删除广告成功");
    }

    /**
     * 修改广告
     * @param adDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AdDto adDto){
        log.info("修改广告信息：{}",adDto);

        advertisingService.updateWithId(adDto);
        return R.success("修改广告成功");
    }

    /**
     * 修改广告时页面的数据展示
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AdDto> getAdById(@PathVariable("id")Long id){
        AdDto adDto = advertisingService.getAdDtoById(id);
        return R.success(adDto);
    }
}
